package com.example.demotest.service;

import com.example.demotest.pojo.ProductFavorite;
import com.example.demotest.pojo.User;
import com.example.demotest.pojo.Product;
import com.example.demotest.repository.ProductFavoriteRepository;
import com.example.demotest.repository.ProductRepository;
import com.example.demotest.repository.UserRepository;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;

import java.util.List;
import java.time.LocalDateTime;
import java.util.stream.Collectors;

@Service
public class ProductFavoriteService {

    private final UserRepository userRepository;
    private final ProductRepository productRepository;
    private final ProductFavoriteRepository productFavoriteRepository;

    public ProductFavoriteService(UserRepository userRepository, ProductRepository productRepository, ProductFavoriteRepository productFavoriteRepository) {
        this.userRepository = userRepository;
        this.productRepository = productRepository;
        this.productFavoriteRepository = productFavoriteRepository;
    }

    /*
     * 用户收藏商品
     */
    @Transactional
    public void addProductFavorite(Integer userId, Integer productId) {

        User user = userRepository.findById(userId).orElseThrow();
        Product product = productRepository.findById(productId).orElseThrow();

        boolean exists = productFavoriteRepository.findByUserAndProduct(user,product).isPresent();
        if(!exists) {
            ProductFavorite productFavorite = new ProductFavorite();
            productFavorite.setUser(user);
            productFavorite.setProduct(product);
            productFavorite.setCreatedAt(LocalDateTime.now());

            productFavoriteRepository.updateFavoriteCount(productId,1); // 商品的被收藏数属性+1
            productFavoriteRepository.save(productFavorite);
        }
        else{
            throw new IllegalArgumentException("用户已经收藏了该商品");
        }

    }

    /*
     * 用户取消关注
     */
    @Transactional
    public void removeProductFavorite(Integer userId, Integer productId) {
        User user = userRepository.findById(userId).orElseThrow();
        Product product = productRepository.findById(productId).orElseThrow();

        boolean exists = productFavoriteRepository.findByUserAndProduct(user,product).isPresent();
        if(exists) {
            productFavoriteRepository.updateFavoriteCount(productId,-1); // 商品的被收藏数属性+1
            productFavoriteRepository.deleteByUserAndProduct(user,product);
        }
        else {
            throw new IllegalArgumentException("用户并没有收藏该商品");
        }
    }

    /*
     * 获取用户收藏列表
     */
    public List<Product> getFavoritesByUserId(Integer userId) {
        User user = userRepository.findById(userId).orElseThrow();
        return productFavoriteRepository.findByUser(user)
                .stream()
                .map(ProductFavorite::getProduct)
                .collect(Collectors.toList());
    }

    @Transactional
    public boolean isProductFavorited(Integer userId, Integer productId) {
        return productFavoriteRepository.existsByUser_UserIdAndProduct_ProductID(userId, productId);
    }



}
